using MediatR;
using Microsoft.AspNetCore.Mvc;
using Medical.ErrorCode;
using BaseData.ErrorCode;
using Medical.ReadApi.Appcontext.Command;
using Medical.ReadApi.Dto;

namespace Medical.ReadApi.Controllers
{
	/// <summary>
	/// 住院单管理控制器（读操作）
	/// </summary>
	[ApiController]
	[Route("api/[controller]/[action]")]
	public class InpatientManagementController : ControllerBase
	{
		private readonly IMediator _mediator;
		
		/// <summary>
		/// 构造函数
		/// </summary>
		/// <param name="mediator">MediatR中介者</param>
		public InpatientManagementController(IMediator mediator)
		{
			_mediator = mediator;
		}

		/// <summary>
		/// 根据ID获取住院单模板详情
		/// </summary>
		/// <param name="templateId">模板ID</param>
		/// <returns>模板详细信息</returns>
		/// <remarks>
		/// 示例请求:
		/// GET /api/InpatientManagement/GetById?templateId=1
		/// </remarks>
		/// <response code="200">查询成功，返回模板详细信息</response>
		/// <response code="400">请求参数错误</response>
		/// <response code="404">模板不存在</response>
		/// <response code="500">服务器内部错误</response>
		[ProducesResponseType(typeof(ApiResult<InpatientOrderTemplateDto>), 200)]
		[ProducesResponseType(400)]
		[ProducesResponseType(404)]
		[ProducesResponseType(500)]
		[HttpGet]
		public async Task<ApiResult<InpatientOrderTemplateDto>> GetById([FromQuery] long templateId)
		{
			try
			{
				var query = new GetInpatientOrderTemplateByIdQuery { TemplateId = templateId };
				var data = await _mediator.Send(query);
				return new ApiResult<InpatientOrderTemplateDto> { Code = ApiEnum.查询成功, Message = "查询成功", data = data };
			}
			catch (Exception ex)
			{
				return new ApiResult<InpatientOrderTemplateDto> { Code = ApiEnum.查询失败, Message = ex.Message, data = null };
			}
		}

		/// <summary>
		/// 获取住院单模板列表（支持分页和筛选）
		/// </summary>
		/// <param name="isActive">是否启用状态筛选</param>
		/// <param name="templateNameKeyword">模板名称关键词（模糊搜索）</param>
		/// <param name="page">页码（从1开始，默认值：1）</param>
		/// <param name="size">每页数量（默认值：10）</param>
		/// <returns>分页的模板列表</returns>
		/// <remarks>
		/// 示例请求:
		/// GET /api/InpatientManagement/GetList?isActive=true&amp;templateNameKeyword=住院&amp;page=1&amp;size=10
		/// </remarks>
		/// <response code="200">查询成功，返回分页的模板列表</response>
		/// <response code="400">请求参数错误</response>
		/// <response code="500">服务器内部错误</response>
		[ProducesResponseType(typeof(ApiResult<ApiPaging<InpatientOrderTemplateDto>>), 200)]
		[ProducesResponseType(400)]
		[ProducesResponseType(500)]
		[HttpGet]
		public async Task<ApiResult<ApiPaging<InpatientOrderTemplateDto>>> GetList([FromQuery] bool? isActive = null, [FromQuery] string? templateNameKeyword = null, [FromQuery] int page = 1, [FromQuery] int size = 10)
		{
			try
			{
				var query = new GetInpatientOrderTemplatesListQuery { IsActive = isActive, TemplateNameKeyword = templateNameKeyword, Page = page, Size = size };
				var data = await _mediator.Send(query);
				return new ApiResult<ApiPaging<InpatientOrderTemplateDto>> { Code = ApiEnum.查询成功, Message = "查询成功", data = data };
			}
			catch (Exception ex)
			{
				return new ApiResult<ApiPaging<InpatientOrderTemplateDto>> { Code = ApiEnum.查询失败, Message = ex.Message, data = null };
			}
		}
	}
}
